package com.longcai.controller.goods;


import com.longcai.entity.homepage.ClyjNearbyBanner;
import com.longcai.service.goods.ClyjNearbyBannerService;

import com.longcai.util.OSSUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.bean.ApiResult;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 附近-banner 前端控制器
 * </p>
 *
 * @author LWK
 * @since 2019-03-20
 */
@Controller
@RequestMapping("/clyjNearbyBanner")
public class ClyjNearbyBannerController {

    @Autowired
    ClyjNearbyBannerService bannerService;

    /**
     * description: 首页banner展示
     * author: LWK
     * time： 09点09分
     */
    @RequestMapping("/getHomepageBannerList")
    @ResponseBody
    public List<ClyjNearbyBanner> getHomepageBannerList() {
        ApiResult<List<ClyjNearbyBanner>> result = new ApiResult<List<ClyjNearbyBanner>>();
        try {
            QueryWrapper<ClyjNearbyBanner> queryWrapper = new QueryWrapper<ClyjNearbyBanner>();
            queryWrapper.eq("status", 0);
            queryWrapper.orderByAsc("banner_sort");
            result.success();
            return bannerService.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode("-1");
            result.fail("查询失败：" + e.getMessage());
        }
        return null;
    }

    /**
     * 插入数据
     *
     * @return
     */
    @PostMapping(value = "/insertBanner")
    @ResponseBody
    public ApiResult insertBanner(HttpServletRequest request, MultipartFile banner) {
        try {
            ClyjNearbyBanner dictInfo = new ClyjNearbyBanner();
            dictInfo.setLink(request.getParameter("link"));
            dictInfo.setCreateTime(LocalDateTime.now());
            dictInfo.setUpdateTime(LocalDateTime.now());
            dictInfo.setStatus(0);
            dictInfo.setTags("1");
            dictInfo.setBannerType(request.getParameter("bannerType"));
            if (request.getParameter("bannerType") == null || request.getParameter("bannerType").length() == 0) {
                return ApiResult.fail("bannerType不能为空");
            }
            dictInfo.setId(UUID.randomUUID().toString());
            dictInfo.setBannerSort(request.getParameter("bannerSort"));
            if (request.getParameter("bannerSort") == null || request.getParameter("bannerSort").length() == 0) {
                return ApiResult.fail("bannerSort不能为空");
            }


            getBannerUrl(dictInfo, banner);
            bannerService.insert(dictInfo);


            return ApiResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ApiResult.fail("字典添加失败！");
        }
    }

    /**
     * 删除数据
     *
     * @return
     */
    @RequestMapping(value = "/deleteBanner")
    @ResponseBody
    public Map<String, Object> deleteMenu(@RequestBody JSONObject params) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            String att = params.getString("rowData");
            att = att.replace("[", "").replace("]", "").replace(" ", "");
            String[] attCode = att.split(",");
            for (String s : attCode) {
                boolean deleteById = bannerService.deleteById(s);
            }
            result.put("message", "删除成功");
            result.put("flag", "S");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            result.put("message", "删除失败");
            return result;
        }
    }

    /**
     * 修改数据
     *
     * @return
     */
    @PostMapping(value = "/updateBanner")
    @ResponseBody
    public Map<String, Object> updateBanner(HttpServletRequest request, MultipartFile banner) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            ClyjNearbyBanner dictInfo = new ClyjNearbyBanner();
            dictInfo.setLink(request.getParameter("link"));
            dictInfo.setUpdateTime(LocalDateTime.now());
            dictInfo.setBannerType(request.getParameter("bannerType"));
            if (request.getParameter("bannerType") == null || request.getParameter("bannerType").length() == 0) {
                result.put("message", "link不能为空");
                return result;
            }
            dictInfo.setId(request.getParameter("id"));
            dictInfo.setBannerSort(request.getParameter("bannerSort"));
            if (request.getParameter("bannerSort") == null || request.getParameter("bannerSort").length() == 0) {
                result.put("message", "link不能为空");
                return result;
            }
            getBannerUrl(dictInfo, banner);
            bannerService.updateById(dictInfo);
            result.put("message", "修改成功");
            result.put("flag", "S");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            result.put("message", "修改失败");
            return result;
        }
    }

    /*
     * 提交的公共部分
     * */
    private static void getBannerUrl(ClyjNearbyBanner dictInfo, MultipartFile banner) {
        try {
            String fileName = banner.getOriginalFilename();
            String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
            InputStream inputStream = null;
            inputStream = banner.getInputStream();
            Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/BANNERSCLASS/");
            dictInfo.setBannerUrl(uploadByInputStream.get("path").toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

